package io.renren.modules.com.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xiaoleilu.hutool.date.DateUtil;
import com.xiaoleilu.hutool.util.CollectionUtil;
import com.xiaoleilu.hutool.util.StrUtil;

import io.renren.common.utils.Constant;
import io.renren.common.utils.R;
import io.renren.modules.com.dao.ComAssetDao;
import io.renren.modules.com.dao.ComContractDao;
import io.renren.modules.com.entity.ComAssetEntity;
import io.renren.modules.com.entity.ComContractAssetEntity;
import io.renren.modules.com.entity.ComContractEntity;
import io.renren.modules.com.service.ComAssetService;

@Service("comAssetService")
public class ComAssetServiceImpl implements ComAssetService {

	@Autowired
	private ComAssetDao comAssetDao;

	@Autowired
	private ComContractDao comContractDao;

	@Override
	public ComAssetEntity queryObject(Long assetId) {
		return comAssetDao.queryObject(assetId);
	}

	@Override
	public List<ComAssetEntity> queryList(Map<String, Object> map) {
		map.put("nowdate", DateUtil.today());
		String rentUser = (String) map.get("rentUser");
		String rentStatus = (String) map.get("rentStatus");
		if (StrUtil.isNotBlank(rentUser)) {
			map.put("condition", 1);
		} else {
			if (StrUtil.equals("0", rentStatus)) {
				map.put("condition", 2);
			}
			if (StrUtil.equals("1", rentStatus)) {
				map.put("condition", 3);
			}
		}
		List<ComAssetEntity> list = comAssetDao.queryList(map);
		for (ComAssetEntity comAssetEntity : list) {
			hasContract(comAssetEntity);
		}

		return list;
	}

	private void hasContract(ComAssetEntity comAssetEntity) {
		ComContractAssetEntity entity = new ComContractAssetEntity();
		entity.setAssetId(Long.valueOf(comAssetEntity.getAssetId()));
		List<ComContractEntity> lists = comContractDao.queryByAssetId(entity);
		if (CollectionUtil.isNotEmpty(lists)) {
			for (ComContractEntity comContractEntity : lists) {
				if (DateUtil.parse(comContractEntity.getEndDate()).getTime() >= DateUtil.parse(DateUtil.today())
						.getTime()
						&& DateUtil.parse(comContractEntity.getStartDate()).getTime() <= DateUtil
								.parse(DateUtil.today()).getTime()) {
					comAssetEntity.setRentStatus(Constant.RENT_STATUS_1);
					comAssetEntity.setRentUser(comContractEntity.getRentUser());
					comAssetEntity.setContractNo(comContractEntity.getContractNo());
					return;
				}
			}
		}
		comAssetEntity.setRentStatus(Constant.RENT_STATUS_0);
		comAssetEntity.setRentUser(null);
		comAssetEntity.setContractNo(null);
		return;
	}

	@Override
	public int queryTotal(Map<String, Object> map) {
		return comAssetDao.queryTotal(map);
	}

	@Override
	public R save(ComAssetEntity comAsset) {
		ComAssetEntity entity = comAssetDao.queryByRoomNum(comAsset);
		if (entity != null) {
			return R.error("该房号已存在");
		}
		comAssetDao.save(comAsset);
		return R.ok();
	}

	@Override
	public R update(ComAssetEntity comAsset) {
		ComAssetEntity entity = comAssetDao.queryByRoomNum(comAsset);
		if (entity != null && entity.getAssetId().longValue() == comAsset.getAssetId().longValue()) {
			return R.error("该房号已存在");
		}
		comAssetDao.update(comAsset);
		return R.ok();
	}

	@Override
	public void delete(Long assetId) {
		comAssetDao.delete(assetId);
	}

	@Override
	public void deleteBatch(Long[] assetIds) {
		comAssetDao.deleteBatch(assetIds);
	}

}
